//
//  ChangeAddressViewController.m
//  Mall
//
//  Created by 孙飞燕 on 16/10/21.
//  Copyright © 2016年 孙飞燕. All rights reserved.
//

#import "ChangeAddressViewController.h"
#import "ChangeAddressFirstTableViewCell.h"
#import "ASSecondTableViewCell.h"
#import "ASThirdTableViewCell.h"
#import "ChangeAddressSecondTableViewCell.h"
@interface ChangeAddressViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic,strong)UITableView *tableView;
@end

@implementation ChangeAddressViewController


- (UITableView *)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc]initWithFrame:self.view.frame];
        _tableView.delegate = self;
        _tableView.dataSource = self;

        //注册cell
        [_tableView registerNib:[UINib nibWithNibName:@"ChangeAddressFirstTableViewCell" bundle:nil] forCellReuseIdentifier:@"ChangeAddressFirstTableViewCell"];
        [_tableView registerNib:[UINib nibWithNibName:@"ASSecondTableViewCell" bundle:nil] forCellReuseIdentifier:@"ASSecondTableViewCell"];
        [_tableView registerClass:[DetailAddressTableViewCell class] forCellReuseIdentifier:@"DetailAddressTableViewCell"];
        [_tableView registerNib:[UINib nibWithNibName:@"ChangeAddressSecondTableViewCell" bundle:nil] forCellReuseIdentifier:@"ChangeAddressSecondTableViewCell"];
        _tableView.backgroundColor = [UIColor groupTableViewBackgroundColor];
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;


    }
    return _tableView;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [self.view addSubview:self.tableView];
    //导航栏右侧保存按钮
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"保存" style:UIBarButtonItemStylePlain target:self action:@selector(saveAction:)];
    // Do any additional setup after loading the view.
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(changeAddressAction:) name:@"ReceiveStr" object:nil];
}

- (void)changeAddressAction:(NSNotification*)notification{

    self.receiveStr = [notification.userInfo objectForKey:@"address"];
    [self.tableView reloadData];
}

- (void)saveAction:(UIBarButtonItem *)sender{
    [self.navigationController popViewControllerAnimated:YES];
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

    return 6;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row == 0 || indexPath.row == 1) {

        ChangeAddressFirstTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ChangeAddressFirstTableViewCell" forIndexPath:indexPath];
        if (indexPath.row == 0) {
            cell.firstLabel.text = @"收货人";
            cell.secondLabel.text = @"孙飞燕";
        }else{
            cell.firstLabel.text = @"联系电话";
            cell.secondLabel.text = @"15771967221";
        }
        return cell;


    }else if (indexPath.row == 2 || indexPath.row == 3){

        ASSecondTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ASSecondTableViewCell" forIndexPath:indexPath];
        if (indexPath.row == 2) {
            cell.firstLabel.text = @"所在地区";
            cell.secondLabel.text = self.receiveStr;
        }else{
            cell.firstLabel.text = @"街道";
            cell.secondLabel.text = @"电子城街道";
        }
        return cell;


    }else if (indexPath.row == 4){

        DetailAddressTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"DetailAddressTableViewCell" forIndexPath:indexPath];
        [cell addSubview:cell.detailAddressTF];
        cell.detailAddressTF.placeholder = @"详细地址";
        return cell;

    }else{
        ChangeAddressSecondTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ChangeAddressSecondTableViewCell" forIndexPath:indexPath];
        return cell;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row == 4) {
        return 100;
    }else{
        return 50;
    }
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    switch (indexPath.row) {
        case 2:
        {
            ChooseAreaViewController *chooseAreaVC = [[ChooseAreaViewController alloc]init];
            [self.navigationController pushViewController:chooseAreaVC animated:YES];
        }
            break;

        default:
            break;
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


-(void)dealloc{
    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"ReceiveStr" object:nil];
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
